package todomanager.main;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;

import todomanager.CONTROLLER.MainFrameController;
import todomanager.MODEL.LanguageModel;
import todomanager.MODEL.TodoManagerModel;
import todomanager.View.MainFrame;
import todomanager.View.ReminderView;
import todomanager.View.SetLanguageFrame;
import todomanager.data.ITask;
import todomanager.data.TodoData;
import todomanager.io.ITodoImporter;
import todomanager.io.TodoXMLReader;
import todomanager.theme.CustomTheme;

/**
 * 
 * @author jonasbirgersson
 */
public class Main {

	private TodoData data;
	private TodoManagerModel modelTodo;
	private MainFrame mainFrameView;
	private MainFrameController mainFrameController;
	private ReminderView reminder;
	private ResourceBundle lang;
	
	// Set to false to ignore custom theme
	private boolean useThemes = true;
	private String currentThemeName = "none";

	public Main() {

	}

	public void start() throws FileNotFoundException, IOException {
		//useThemes = false;

		if (useThemes) {
			// Current themes are: uglytheme, testtheme
			Properties appProperties = new Properties();
			FileInputStream fis = new FileInputStream("todo.properties");
			appProperties.load(fis);
			
			this.currentThemeName = appProperties.getProperty("currentTheme", "uglytheme");
			fis.close();
			
			
			MetalTheme theme = new CustomTheme(this.currentThemeName);

			
			// set the chosen theme
			MetalLookAndFeel.setCurrentTheme(theme);
			try {
				UIManager.setLookAndFeel(new MetalLookAndFeel());
			} catch (Exception e) {
				System.out.println(e);
			}
		}
		/*
		lang = ResourceBundle.getBundle(
				"todomanager.localization.Language", Locale.ROOT);*/
		SetLanguageFrame langframe = new SetLanguageFrame();
		langframe.setVisible(true);
		
		while(!langframe.isSelected()){
		
				System.out.println("");
			
		}
		
		lang = langframe.getResource();

		langframe.setVisible(false);
		
		modelTodo = new TodoManagerModel();
		data = new TodoData();
		
		File databaseFile = new File("database.xml");
		if (databaseFile.exists()) {
			ITodoImporter read = new TodoXMLReader(new BufferedReader(
					new FileReader("database.xml")));

			ArrayList<ITask> taskList = read.importTodo();
			Iterator<ITask> tli = taskList.iterator();
			while (tli.hasNext()) {
				data.addTask(tli.next());
			}
		}

		modelTodo.addTodoData(data);
		mainFrameView = new MainFrame(modelTodo, lang);
		mainFrameView.setCurrentTheme(this.currentThemeName);		

		mainFrameController = new MainFrameController(modelTodo, mainFrameView);

		mainFrameView.setTitle("TODO manager");
		mainFrameView.setVisible(true);


		reminder = new ReminderView(modelTodo);
		reminder.setVisible(true);

	}

	public static void main(String[] args) throws FileNotFoundException,
			IOException {

		Main todomanager = new Main();
		todomanager.start();

	}
	public void setLang(ResourceBundle language){
		lang = language;
	}
}
